package com.zxh.okretrofit;

import android.support.annotation.NonNull;

import com.zxh.okretrofit.constant.HttpConfig;
import com.zxh.okretrofit.schedulers.RxSchedulersHelper;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.Headers;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by zhangxuehui on 2018/2/8.
 */

public class OkRetrofit implements HttpEngine{
    private static OkRetrofit INSTANCE = null;
    private OkHttpClient mOkHttpClient;//配置OkHttpClient
    private Retrofit mRetrofit;//配置OkHttpClient
    private ApiService mApiService;//配置OkHttpClient

    public static OkRetrofit getInstance() {
        if (INSTANCE == null) {
            synchronized (OkRetrofit.class) {
                if (INSTANCE == null) {
                    synchronized (OkRetrofit.class) {
                        INSTANCE = new OkRetrofit();
                    }
                }
            }
        }
        return INSTANCE;
    }

    private OkRetrofit() {
        mApiService = getRetrofit(getOkHttpClient()).create(ApiService.class);
    }

//    private String baseUrl;//api根地址
//    private long connectTimeout;//连接时间
//    private long writeTimeout;//写入时间
//    private long readTimeout;//读取时间
//    private Headers.Builder headers;//
//    private List<Interceptor> interceptors;//普通拦截器，请求前调用
//    private List<Interceptor> netInterceptors;//网络拦截器，请求过程中调用

    /**
     * 获取OkHttpClient()
     *
     * @return
     */
    public OkHttpClient getOkHttpClient() {
        mOkHttpClient = new OkHttpClient.Builder()
                .writeTimeout(HttpConfig.writeTimeout, TimeUnit.SECONDS)
                .connectTimeout(HttpConfig.connectTimeout, TimeUnit.SECONDS)
                .readTimeout(HttpConfig.readTimeout, TimeUnit.SECONDS)
                .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
                .build();
        return mOkHttpClient;
    }

    /**
     * 获取retrofit
     *
     * @param okHttpClient
     * @return
     */
    public Retrofit getRetrofit(@NonNull OkHttpClient okHttpClient) {
        mRetrofit = new Retrofit.Builder()
                .client(okHttpClient)
//                .callbackExecutor()//callback线程池管理
//                .callFactory()//负责创建Http请求
                .validateEagerly(true)//预处理handler
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        return mRetrofit;
    }

    /**
     * 获取service服务
     *
     * @return
     */
    public ApiService getService() {
        if (mApiService == null) {
            mApiService = mRetrofit.create(ApiService.class);
        }
        return mApiService;
    }

    @Override
    public void post(Map<String, String> params, String url, HttpCallBack httpCallBack) {
        ApiService service = getService();
        service.rxGetData(url)
                .compose(RxSchedulersHelper.io_main())
                .subscribe()
    }

    @Override
    public void get(Map<String, String> params, String url, HttpCallBack httpCallBack) {

    }

    @Override
    public void post(Map<String, String> params, String url, HttpCallBack httpCallBack, boolean isCache) {

    }

    @Override
    public void get(Map<String, String> params, String url, HttpCallBack httpCallBack, boolean isCache) {

    }
}
